package com.http;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.http.ClientInfoHolder;
import com.http.ClientInfo;

public class ClientInfoHolder 
{
	 private static final ThreadLocal clientInfoHolder = new ThreadLocal();
	 
	 public ClientInfoHolder()
     {
     }

     @SuppressWarnings("unchecked")
	public static void setClientInfo(ClientInfo clientInfo)
     {
         clientInfoHolder.set(clientInfo);
     }

     public static ClientInfo getClientInfo()
     {
         return (ClientInfo)clientInfoHolder.get();
     }

     public static ClientInfo getClientInfo(HttpServletRequest request, String alternateLocation)
     {
        if(StringUtils.isNotEmpty(alternateLocation))
        {
            String otherHeader[] = alternateLocation.split(",");
            if(otherHeader != null && otherHeader.length > 0)
            {
                String targetHeader = "";
                String as[];
                int j = (as = otherHeader).length;
                for(int i = 0; i < j; i++)
                {
                    String header = as[i];
                    if(request.getHeader(header) == null)
                        continue;
                    targetHeader = header;
                    break;
                }

                return new ClientInfo(request, targetHeader);
            }
        }
        return null;
     }

     public static void clear()
     {
         clientInfoHolder.remove();
     }
     
}
